package com.tensquare.friend.dao;

import com.tensquare.friend.pojo.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * @author: WangYaWei
 * @description:
 * @create: 2019-08-26 16:37
 **/
public interface FriendDao extends JpaRepository<Friend, String>, JpaSpecificationExecutor<Friend> {

    /**
     * 根据用户id与被喜欢用户id查询记录条数
     * @param userid
     * @param friendid
     * @return
     */
    @Query(value = "SELECT COUNT(1) FROM Friend f WHERE f.userid = ?1 AND f.friendid = ?2")
    public int findCountByUseridAndFriendid(String userid, String friendid);

    /**
     * 更新为互相喜欢
     * @param userid
     * @param friendid
     * @param islike
     * @return
     */
    @Modifying
    @Query("UPDATE Friend f SET f.islike = :islike WHERE f.userid = :userid AND f.friendid = :friendid")
    public int updateLike(@Param("userid") String userid, @Param("friendid") String friendid, @Param("islike") String islike );

    /**
     * 删除好友
     * @param userid    用户id
     * @param friendid  好友id
     */
    @Modifying
    @Query("DELETE FROM Friend WHERE userid = ?1 AND friendid = ?2")
    int deleteByUseridAndFriendid(String userid, String friendid);
}
